package com.jin.webvideo.externalInterfaces;

import java.lang.reflect.Field;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class PlatformSupport {

    public static final String PLATFORM_IMGO = "imgo";
    public static final String PLATFORM_NAME_IMGO = "芒果TV";
    public static final String HOST_KEY_IMGO = "mgtv.com";

    public static final String PLATFORM_IQIYI = "iqiyi";
    public static final String PLATFORM_NAME_IQIYI = "爱奇艺";
    public static final String HOST_KEY_IQIYI = "iqiyi.com";

    public static final String PLATFORM_QQ = "qq";
    public static final String PLATFORM_NAME_QQ = "腾讯视频";
    public static final String HOST_KEY_QQ = "qq.com";

    public static final String PLATFORM_YOUKU = "youku";
    public static final String PLATFORM_NAME_YOUKU = "优酷";
    public static final String HOST_KEY_YOUKU = "youku.com";

    public static final String PLATFORM_MIGU = "migu";
    public static final String PLATFORM_NAME_MIGU = "咪咕视频";
    public static final String HOST_KEY_MIGU = "miguvideo.com";

    public static final String PLATFORM_BILIBILI = "bilibili";
    public static final String PLATFORM_NAME_BILIBILI = "哔哩哔哩";
    public static final String HOST_KEY_BILIBILI = "bilibili.com";

    public static final String PLATFORM_NETFLY = "netfly";
    public static final String PLATFORM_NAME_NETFLY = "奈飞";
    public static final String HOST_KEY_NETFLY = "netfly.tv";

    public static final String PLATFORM_LOCAL = "local";
    public static final String PLATFORM_NAME_LOCAL = "站点自有";

    public static final String PLATFORM_YZZY = "YZZY";
    public static final String PLATFORM_NAME_YZZY = "优质资源";

    public static final String PLATFORM_UNKNOW = "unknow";

    private static Map<String, String> platformMap = new HashMap<>();
    private static Map<String, String> hostMap = new HashMap<>();

    static {
        try {
            Class clazz = PlatformSupport.class;
            Field[] fields = clazz.getFields();
            for (Field field : fields) {
                String name = field.getName();
                if (name.startsWith("PLATFORM_NAME")) {
                    continue;
                }
                if (name.startsWith("PLATFORM_")) {
                    String value = (String) field.get(null);
                    String key = name.substring(8);
                    Field keyField = getField(clazz, "PLATFORM_NAME" + key);
                    if (keyField != null) {
                        platformMap.put(value, (String) keyField.get(null));
                    }
                    keyField = getField(clazz, "HOST_KEY" + key);
                    if (keyField != null) {
                        hostMap.put((String) keyField.get(null), value);
                    }
                }
            }
        }
        catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
    private static Field getField(Class clazz, String name) {
        Field field = null;
        try {
            field = clazz.getField(name);
        } catch (NoSuchFieldException e) {
        }
        return field;
    }
    public static boolean isSupport(String platform){
        return platformMap.containsKey(platform);
    }
    public static String platformName(String platform){
        return platformMap.getOrDefault(platform, "");
    }
    // 无法判断loacl
    public static String getPlatform(String url){
        String platform = PLATFORM_UNKNOW;
        try {
            URI uri = new URI(url);
            String host = uri.getHost();
            for (String key : hostMap.keySet()) {
                if (url.indexOf(key) != -1) {
                    return hostMap.get(key);
                }
            }
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return platform;
    }
}
